<template>
	<view class="container">
		<!-- 顶部筛选 -->
		<view class="filter-box">
			<view 
				class="filter-item"
				v-for="(item, index) in filterList"
				:key="index"
				:class="{ active: currentFilterIndex === index }"
				@click="selectFilter(index)"
			>
				<text>{{ item.name }}</text>
				<text class="iconfont" :class="getFilterIcon(index)"></text>
			</view>
		</view>
		
		<!-- 商品列表 -->
		<view class="product-list">
			<view 
				class="product-item"
				v-for="(item, index) in productList"
				:key="index"
				@click="goDetail(item)"
			>
				<image :src="item.image" mode="aspectFill"></image>
				<view class="info">
					<text class="name">{{ item.name }}</text>
					<view class="price-box">
						<text class="price">¥{{ item.price }}</text>
						<text class="sales">已售{{ item.sales }}</text>
					</view>
				</view>
			</view>
		</view>
		
		<!-- 空状态 -->
		<view class="empty-box" v-if="productList.length === 0 && !loading">
			<image src="/static/images/empty.png" mode="aspectFit"></image>
			<text>暂无商品</text>
		</view>
		
		<!-- 加载中 -->
		<view class="loading-box" v-if="loading">
			<text>正在加载...</text>
		</view>
		
		<!-- 无更多数据 -->
		<view class="no-more-box" v-if="!hasMore && productList.length > 0 && !loading">
			<text>没有更多商品了</text>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			categoryId: 0,
			categoryName: '',
			currentFilterIndex: 0,
			filterList: [
				{
					name: '综合',
					sortField: '',
					sortOrder: ''
				},
				{
					name: '销量',
					sortField: 'sales',
					sortOrder: 'desc'
				},
				{
					name: '价格',
					sortField: 'price',
					sortOrder: ''
				}
			],
			productList: [],
			page: 1,
			pageSize: 10,
			total: 0,
			hasMore: true,
			loading: false
		}
	},
	onLoad(options) {
		if (options.category) {
			this.categoryId = options.category
			this.categoryName = options.name || '商品列表'
			uni.setNavigationBarTitle({
				title: this.categoryName
			})
		}
		this.getProductList(true)
	},
	onPullDownRefresh() {
		this.page = 1
		this.getProductList(true)
	},
	onReachBottom() {
		if (this.hasMore) {
			this.page++
			this.getProductList()
		}
	},
	methods: {
		selectFilter(index) {
			// 如果点击的是价格，则切换排序方式
			if (index === 2) {
				const currentOrder = this.filterList[2].sortOrder
				this.filterList[2].sortOrder = currentOrder === 'asc' ? 'desc' : 'asc'
			}
			
			this.currentFilterIndex = index
			this.page = 1
			this.getProductList(true)
		},
		getFilterIcon(index) {
			if (index !== 2) return ''
			
			const currentOrder = this.filterList[2].sortOrder
			if (!currentOrder) return 'icon-sort'
			return currentOrder === 'asc' ? 'icon-sort-asc' : 'icon-sort-desc'
		},
		getProductList(refresh = false) {
			if (this.loading) return
			
			this.loading = true
			
			// 获取当前筛选条件
			const currentFilter = this.filterList[this.currentFilterIndex]
			
			// 模拟获取商品列表
			setTimeout(() => {
				// 模拟数据
				const data = [
					{
						id: 1,
						name: '新鲜草莓',
						price: '38.8',
						sales: 999,
						image: '/static/product/strawberry.jpg'
					},
					{
						id: 2,
						name: '有机生菜',
						price: '12.8',
						sales: 888,
						image: '/static/product/lettuce.jpg'
					},
					{
						id: 3,
						name: '精选牛肉',
						price: '88.8',
						sales: 777,
						image: '/static/product/beef.jpg'
					},
					{
						id: 4,
						name: '深海鲜虾',
						price: '68.8',
						sales: 666,
						image: '/static/product/shrimp.jpg'
					}
				]
				
				if (refresh) {
					this.productList = data
				} else {
					this.productList = [...this.productList, ...data]
				}
				
				this.total = 20
				this.hasMore = this.productList.length < this.total
				this.loading = false
				
				uni.stopPullDownRefresh()
			}, 500)
		},
		goDetail(item) {
			uni.navigateTo({
				url: `/pages/product/detail?id=${item.id}`
			})
		}
	}
}
</script>

<style lang="scss">
.container {
	background-color: #f5f5f5;
	min-height: 100vh;
}

.filter-box {
	position: sticky;
	top: 0;
	left: 0;
	z-index: 999;
	display: flex;
	background-color: #fff;
	padding: 20rpx 0;
	margin-bottom: 20rpx;
	box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
	
	.filter-item {
		flex: 1;
		display: flex;
		justify-content: center;
		align-items: center;
		
		text {
			font-size: 28rpx;
			color: #666;
		}
		
		.iconfont {
			margin-left: 5rpx;
		}
		
		&.active {
			text {
				color: #ff5151;
			}
		}
	}
}

.product-list {
	padding: 0 20rpx;
	display: flex;
	flex-wrap: wrap;
	
	.product-item {
		width: calc(50% - 20rpx);
		margin: 10rpx;
		background-color: #fff;
		border-radius: 16rpx;
		overflow: hidden;
		
		image {
			width: 100%;
			height: 320rpx;
		}
		
		.info {
			padding: 16rpx;
			
			.name {
				font-size: 28rpx;
				color: #333;
				overflow: hidden;
				text-overflow: ellipsis;
				display: -webkit-box;
				-webkit-line-clamp: 2;
				-webkit-box-orient: vertical;
				line-height: 1.3;
				height: 2.6em;
				margin-bottom: 16rpx;
			}
			
			.price-box {
				display: flex;
				justify-content: space-between;
				align-items: center;
				
				.price {
					font-size: 32rpx;
					font-weight: bold;
					color: #ff5151;
				}
				
				.sales {
					font-size: 22rpx;
					color: #999;
				}
			}
		}
	}
}

.empty-box,
.loading-box,
.no-more-box {
	display: flex;
	flex-direction: column;
	justify-content: center;
	align-items: center;
	padding: 100rpx 0;
	
	image {
		width: 240rpx;
		height: 240rpx;
		margin-bottom: 20rpx;
	}
	
	text {
		font-size: 28rpx;
		color: #999;
	}
}

.loading-box {
	padding: 30rpx 0;
}

.no-more-box {
	padding: 30rpx 0;
}
</style> 